<template>
  <div class="products-page">
    <front-header></front-header>
    <div class="main-content">
      <!-- 筛选区域 -->
      <div class="filter-section">
        <el-card shadow="never">
          <!-- 分类筛选 -->
          <div class="filter-group">
            <div class="filter-label">分类：</div>
            <div class="filter-options">
              <el-tag :effect="selectedCategory === '' ? 'dark' : 'plain'" @click="handleCategoryChange('')" class="filter-tag">全部</el-tag>
              <el-tag v-for="category in categories" :key="category.id" :effect="selectedCategory === category.id ? 'dark' : 'plain'"
                @click="handleCategoryChange(category.id)" class="filter-tag">{{ category.name }}</el-tag>
            </div>
          </div>

          <!-- 价格区间 -->
          <div class="filter-group">
            <div class="filter-label">价格：</div>
            <div class="filter-options">
              <el-tag :effect="priceRange === '' ? 'dark' : 'plain'" @click="handlePriceRangeChange('')" class="filter-tag">全部</el-tag>
              <el-tag v-for="(range, index) in priceRanges" :key="index" :effect="priceRange === range.value ? 'dark' : 'plain'"
                @click="handlePriceRangeChange(range.value)" class="filter-tag">{{ range.label }}</el-tag>
            </div>
          </div>
        </el-card>
      </div>

      <!-- 排序和搜索区域 -->
      <div class="toolbar">
        <div class="sort-options">
          <el-radio-group v-model="sortBy" size="small">
            <el-radio-button 
              v-for="option in sortOptions" 
              :key="option.value" 
              :label="option.value"
            >{{ option.label }}</el-radio-button>
          </el-radio-group>
        </div>
        <div class="search-box">
          <el-input 
            v-model="searchKeyword" 
            placeholder="搜索商品" 
            prefix-icon="el-icon-search" 
            clearable 
          ></el-input>
        </div>
      </div>

      <!-- 商品列表 -->
      <div class="products-grid" v-loading="loading">
        <el-row :gutter="20" v-if="products.length > 0">
          <el-col :span="6" v-for="product in products" :key="product.id">
            <product-card :product="product"  />
          </el-col>
        </el-row>
        <div v-else class="empty-state">
          <i class="el-icon-goods"></i>
          <p>暂无商品</p>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination background :current-page.sync="currentPage" :page-size="pageSize" :total="total" layout="prev, pager, next, jumper"
          @current-change="handlePageChange">
        </el-pagination>
      </div>
    </div>
    <front-footer></front-footer>
  </div>
</template>

<script>
import FrontHeader from '@/components/front/FrontHeader.vue'
import FrontFooter from '@/components/front/FrontFooter.vue'
import ProductCard from '@/components/front/ProductCard.vue'
import Request from '@/utils/request'
import { debounce } from 'lodash'

export default {
  name: 'Products',
  components: {
    FrontHeader,
    FrontFooter,
    ProductCard
  },
  data() {
    return {
      loading: false,
      products: [],
      categories: [],
      selectedCategory: '',
      priceRange: '',
      priceRanges: [
        { label: '0-50元', value: '0-50' },
        { label: '50-100元', value: '50-100' },
        { label: '100-200元', value: '100-200' },
        { label: '200元以上', value: '200-' }
      ],
      sortOptions: [
        { label: '默认排序', value: 'default' },
        { label: '销量优先', value: 'sales,desc' },
        { label: '价格从低到高', value: 'price,asc' },
        { label: '价格从高到低', value: 'price,desc' }
      ],
      sortBy: 'default',
      searchKeyword: '',
      currentPage: 1,
      pageSize: 12,
      total: 0,
      debouncedSearch: null
    }
  },
  methods: {
    // 获取商品分类
    async getCategories() {
      try {
        const res = await Request.get('/category/all')
        if (res.code === '0') {
          this.categories = res.data
        }
      } catch (error) {
        console.error('获取分类失败:', error)
      }
    },
    // 获取商品列表
    async getProducts() {
      this.loading = true
      try {
        const params = {
          status: 1,
          currentPage: this.currentPage,
          size: this.pageSize
        }

        // 添加分类筛选
        if (this.selectedCategory) {
          params.categoryId = this.selectedCategory
        }

        // 添加价格区间筛选
        if (this.priceRange) {
          const [min, max] = this.priceRange.split('-')
          if (min) params.minPrice = min
          if (max) params.maxPrice = max
        }

        // 添加排序
        if (this.sortBy !== 'default') {
          const [field, order] = this.sortBy.split(',')
          params.sortField = field
          params.sortOrder = order
        }

        // 添加搜索关键词
        if (this.searchKeyword) {
          params.name = this.searchKeyword
        }

        const res = await Request.get('/product/page', { params })
        if (res.code === '0') {
          if (res.data && res.data.records) {
            this.products = res.data.records.map(product => ({
              ...product,
              isFavorite: false,
              imageUrl: product.imageUrl?.startsWith('http') ? product.imageUrl : `${product.imageUrl}`
            }))
            this.total = res.data.total
          } else {
            this.products = []
            this.total = 0
          }
        } else {
          this.products = []
          this.total = 0
        }
      } catch (error) {
        console.error('获取商品列表失败:', error)
        this.$message.error('获取商品列表失败')
        this.products = []
        this.total = 0
      } finally {
        this.loading = false
      }
    },
    handleCategoryChange(categoryId) {
      this.selectedCategory = categoryId
      this.currentPage = 1
      this.getProducts()
    },
    handlePriceRangeChange(range) {
      this.priceRange = range
      this.currentPage = 1
      this.getProducts()
    },
    handleSearch() {
      this.debouncedSearch()
    },
    handlePageChange(page) {
      this.getProducts()
    },
    // 监听路由变化
    handleRouteChange() {
      const query = {}
      if (this.selectedCategory) {
        query.category = this.selectedCategory
      }
      if (this.searchKeyword) {
        query.keyword = this.searchKeyword
      }
      // 更新URL，但不触发路由变化
      this.$router.replace({ query }).catch(() => {})
    }
  },
  watch: {
    // 监听搜索关键词变化
    searchKeyword() {
      this.handleSearch()
      this.handleRouteChange()
    },
    
    // 监听分类变化
    selectedCategory() {
      this.handleRouteChange()
    },
    sortBy() {
      this.currentPage = 1
      this.getProducts()
    }
  },
  created() {
    // 创建防抖的搜索函数
    this.debouncedSearch = debounce(() => {
      this.currentPage = 1
      this.getProducts()
    }, 300)

    this.getCategories()
    this.getProducts()
    
    // 从URL获取查询参数
    const { category, keyword } = this.$route.query
    if (category) this.selectedCategory = category
    if (keyword) {
      this.searchKeyword = keyword
      this.handleSearch()
    }
  },
  beforeDestroy() {
    // 清除防抖函数
    if (this.debouncedSearch) {
      this.debouncedSearch.cancel()
    }
  }
}
</script>

<style scoped>
.products-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #e8f5e9 0%, #fffde7 100%);
}

.main-content {
  flex: 1;
  padding: 30px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
}

/* 筛选区域样式 */
.filter-section {
  margin-bottom: 30px;
}

.filter-section :deep(.el-card) {
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(103,194,58,0.10) !important;
  background: linear-gradient(135deg, #e8f5e9 0%, #fffde7 100%);
  transition: all 0.3s ease;
}

.filter-section :deep(.el-card:hover) {
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 12px 32px rgba(103,194,58,0.16) !important;
}

.filter-group {
  display: flex;
  align-items: center;
  padding: 16px 0;
}

.filter-group:not(:last-child) {
  border-bottom: 1px solid rgba(103,194,58,0.08);
}

.filter-label {
  width: 80px;
  color: #388e3c;
  font-size: 15px;
  font-weight: 600;
  line-height: 32px;
}

.filter-options {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

.filter-tag {
  cursor: pointer;
  user-select: none;
  margin: 0;
  padding: 0 18px;
  border-radius: 22px;
  font-size: 15px;
  font-weight: 600;
  color: #43a047;
  background: #e8f5e9;
  border: 1.5px solid #a5d6a7;
  transition: all 0.3s ease;
  height: 34px;
  line-height: 34px;
  display: flex;
  align-items: center;
}

.filter-tag:hover {
  background: #c8e6c9;
  color: #388e3c;
}

:deep(.el-tag--dark) {
  background: linear-gradient(90deg, #67c23a 60%, #a5d6a7 100%) !important;
  color: #fff !important;
  border: 1.5px solid #67c23a !important;
  font-weight: 700;
  box-shadow: 0 2px 8px #a5d6a733;
}
:deep(.el-tag--dark:hover) {
  background: linear-gradient(90deg, #43a047 0%, #67c23a 100%) !important;
  color: #fff !important;
}

/* 工具栏样式 */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  background: linear-gradient(90deg, #e8f5e9 60%, #fffde7 100%);
  padding: 18px 28px;
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(103,194,58,0.08);
  gap: 20px;
}

.sort-options :deep(.el-radio-group) {
  display: flex;
  gap: 8px;
}

.sort-options :deep(.el-radio-button__inner) {
  border-radius: 20px;
  padding: 0 20px;
  height: 34px;
  line-height: 34px;
  font-size: 15px;
  font-weight: 600;
  color: #43a047;
  background: #e8f5e9;
  border: 1.5px solid #a5d6a7;
  transition: all 0.3s ease;
}

.sort-options :deep(.el-radio-button:not(:last-child) .el-radio-button__inner) {
  border-right: 1px solid #a5d6a7;
}

.sort-options :deep(.el-radio-button__orig-radio:checked + .el-radio-button__inner) {
  background: linear-gradient(90deg, #67c23a 60%, #a5d6a7 100%);
  border-color: #67c23a;
  color: #fff;
  box-shadow: -1px 0 0 0 #67c23a;
}

/* 搜索框样式优化 */
.search-box {
  width: 320px;
  position: relative;
}

.search-box :deep(.el-input__inner) {
  border-radius: 24px;
  padding-left: 45px;  /* 为搜索图标留出空间 */
  height: 40px;
  line-height: 40px;
  font-size: 15px;
  border: 2px solid #a5d6a7;
  background: #f9fbe7;
  transition: all 0.3s ease;
}

.search-box :deep(.el-input__inner:hover) {
  background: #e8f5e9;
}

.search-box :deep(.el-input__inner:focus) {
  background: #fff;
  border-color: #67c23a;
  box-shadow: 0 0 0 2px rgba(103, 194, 58, 0.13);
}

.search-box :deep(.el-input__prefix) {
  left: 16px;
  line-height: 40px;
}

.search-box :deep(.el-input__icon) {
  font-size: 18px;
  color: #909399;
  transition: all 0.3s ease;
}

.search-box :deep(.el-input__inner:focus + .el-input__prefix .el-input__icon) {
  color: #67c23a;
}

.search-box :deep(.el-input-group__append) {
  display: none;  /* 移除搜索按钮，使用图标代替 */
}

/* 商品网格样式 */
.products-grid {
  min-height: 400px;
  margin: 0 -10px;
  position: relative;
}
.products-grid::before {
  content: '';
  position: absolute;
  left: 18px;
  top: 18px;
  width: 48px;
  height: 48px;
  background: url('data:image/svg+xml;utf8,<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><ellipse cx="24" cy="24" rx="22" ry="10" fill="%23e8f5e9"/><path d="M10 34 Q24 8 38 34" stroke="%2367c23a" stroke-width="2" fill="none"/><ellipse cx="24" cy="24" rx="4" ry="8" fill="%23a5d6a7"/></svg>') no-repeat center/contain;
  opacity: 0.13;
  z-index: 0;
}

.el-col {
  padding: 10px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin: 40px 0;
  padding: 20px 0;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled).active) {
  background-color: #67c23a;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled):hover) {
  color: #67c23a;
}

/* 加载状态样式 */
:deep(.el-loading-mask) {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(4px);
}

/* 响应式布局 */
@media (max-width: 1440px) {
  .main-content {
    max-width: 1200px;
  }
}

@media (max-width: 1200px) {
  .main-content {
    max-width: 960px;
  }
}

@media (max-width: 900px) {
  .main-content {
    padding: 0 4px;
  }
  .filter-section :deep(.el-card) {
    border-radius: 10px;
  }
  .toolbar {
    border-radius: 8px;
    padding: 8px 4px;
  }
}

/* 空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  color: #909399;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-state p {
  font-size: 16px;
  margin: 0;
}
</style> 